﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using User_subsystem;
using web_search;

namespace BookManageSystem
{
    public partial class FormBookDetailsForUser : Form
    {
        public FormBookDetailsForUser()
        {
            InitializeComponent();

            LabelTitle.Text = FormBookBorrowForUser.BookName;
            
            LabelISBN.Text = FormBookBorrowForUser.ISBN;           
            LabelPrice.Text = FormBookBorrowForUser.Price.ToString();           
        }

      
        private void LoadBookCirculation()
        {
           
            dgvBookCirculation.Rows.Clear();

            Daouser dao = new Daouser();
            dao.connect();
            string sql = $"select BookID,BookStatus from  V_UserBookInfor  where BookID Like '{LabelISBN.Text}-%'";
            SqlDataReader reader = dao.read(sql);


            while (reader.Read())
            {
                string bookstatus = "";
                if (reader[1].ToString() == "0")
                {
                    bookstatus = "在馆";
                }
                else if (reader[1].ToString() == "1")
                {
                    bookstatus = "已借出";
                }
                dgvBookCirculation.Rows.Add(reader[0].ToString(),bookstatus);
            }
            reader.Close();
            dao.DaoClose();
        }

        private void FormBookInformationForUser_Load(object sender, EventArgs e)
        {
            LoadBookCirculation();
        }

        private void dgvBookCirculation_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //选中当前的BookID
            if (dgvBookCirculation.CurrentRow == null || dgvBookCirculation.CurrentRow.Cells[0].Value == null)
            {
                return;
            }

            //获取当前选中的行的BookID
            string BookID = dgvBookCirculation.CurrentRow.Cells[0].Value.ToString();

            //显示当前选中的BookID
            LabelBookID.Text = BookID;
        }

        private void button2_Click(object sender, EventArgs e)
        {//借阅图书

            string userid = FormLogin.id;
            string Name = FormLogin.name;

            if (LabelBookID.Text == "NULL")
            {
                MessageBox.Show("请选择图书！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                //获取当前选中的图书的状态
                string status = dgvBookCirculation.CurrentRow.Cells[1].Value.ToString();
                if (status == "在馆")
                {//如果图书在馆，可以借阅
                    Daouser dao = new Daouser();
                    dao.connect();
                    //获取当前日期，用作借阅编号BorrowID
                    DateTime dt = DateTime.Now;
                    string current_date = dt.ToString("yyyyMMdd-HHmmss");
                    //插入借阅记录 规定借阅时间为3个月
                    string sql = $"insert into BorrowRec values('{current_date}','{LabelBookID.Text}','{userid}','{dt}','{dt.AddMonths(3)}',NULL)";
                    if (dao.Execute(sql) > 0)
                    {
                         MessageBox.Show("借阅成功！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                         
                        //更新一下当前状态
                         LoadBookCirculation();
                    }  
                    else
                    {
                        MessageBox.Show("借阅失败！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else 
                {
                    MessageBox.Show("该图书不在馆，无法借阅！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private void label29_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
